<?php
//会员核心数据查询类
namespace Org\Yike;
class Member{

    static public function getCate($model='',$key=1,$data=array(),$field='id,name,alias,pid'){
        $list = M($model)->where(array('status'=>1))->order('ordid desc,id asc')->field($field)->select();
        $category = new Category();
        $listArr = array();
        if($key==1){
            $listArr = $category->unLimitedForLevel($list);
        }else if($key==2){
            $listArr = $category->umLimitedForLayer($list);
        }else if($key==3){
            $listArr = $category->getParents($list,$data['id']);
        }else if($key==4){
            $listArr = $category->getChiIds($list,$data['pid'],1);
        }else if($key==5){
            $listArr = $category->getCateByPid($list,$data['pid']);
        }
        return $listArr;
    }

    //获取会员团报名
    static public function getGroupMess($pageSize=15,$where=array('oh.type'=>4),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("order_house as oh")
                ->join('left join '.$fex.'house as h on h.id = oh.house_id')
                ->field('oh.*,h.title')
                ->where($where)
                ->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("order_house as oh");
        $model->join('left join '.$fex.'house as h on h.id = oh.house_id');
        $model->join('left join '.$fex.'group as g on g.id = oh.group_id');
        $model->field('oh.*,h.title as house_title,g.title as group_title');
        $model->where($where);
        $model->order('oh.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取会员新房留言
    static public function getHouseMess($pageSize=15,$where=array('oh.type'=>array('neq',4)),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("order_house as oh")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("order_house as oh");
        $model->join('left join '.$fex.'house as h on h.id = oh.house_id');
        $model->field('oh.*,h.title as house_title');
        $model->where($where);
        $model->order('oh.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取会员二手房留言
    static public function getUsedMess($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("order_used as ou")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("order_used as ou");
        $model->join('left join '.$fex.'used as u on u.id = ou.used_id');
        $model->field('ou.*,u.id as used_id,u.title as used_title');
        $model->where($where);
        $model->order('ou.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        //echo $model->getLastSql();die;
        return $arr;
    }
    //获取房源跟进表
    static public function getFollow($pageSize=15,$where=array(),$page='',$type=1){

    }

    //获取出租房留言
    static public function getRentalMess($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("order_rental as orr")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("order_rental as orr");
        $model->join('left join '.$fex.'rental as r on r.id = orr.rental_id');
        $model->field('orr.*,r.id as rental_id,r.title as rental_title');
        $model->where($where);
        $model->order('orr.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取出租房列表
    static public function getRentalList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("rental as r")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("rental as r");
        $model->join('left join '.$fex.'area_cate as ac on r.area_id = ac.id');
        $model->field('r.*,ac.name');
        $model->where($where);
        $model->order('r.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取二手房列表
    static public function getUsedList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("used as u")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("used as u");
        $model->join('left join '.$fex.'area_cate as ac on u.area_id = ac.id');
        $model->field('u.*,ac.name');
        $model->where($where);
        $model->order('u.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }
    //新获取房源列表
    static public function newgetUsedList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        if($page){
            $count = M("used as u")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("used as u");
        $model->field('u.*,yk_user.username,yk_room.rid,yk_room.alias,yk_room.f_alias,yk_build.alias as b_alias');
        $model->join('yk_user on u.user_id=yk_user.id','LEFT');
        $model->join('yk_room on u.dict_id=yk_room.rid','LEFT');
        $model->join('yk_build on yk_room.build=yk_build.bid','LEFT');
        $model->where($where);
        $model->order('u.add_time desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }
    //搜索房源
    static public function retrieve($data,$page='',$pageSize=15){
        $map = array ();
        //$where = array ();
        if (!empty ($data['title'])){
            $map['u.title']  = array('like', '%'.$data['title'].'%');
            //$map['_complex'] = $where;
        }
        if (!empty ($data['build'])) $map['bu.build'] = $data['build'];
        if (!empty ($data['owner'])) $map['_string'] = 'u.contacts like "%'.$data['owner'].'%" or u.phone like "%'.$data['owner'].'%"';
        if (!empty ($data['sell_type'] && $data['sell_type']!=0)) $map['u.sell_type'] = $data['sell_type'];
        if (!empty ($data['floor'])){
            $floor = explode('-', $data['floor']);
            if (count ($floor) == 2){
                $map['r.floor'] = array('between',array ($floor[0],$floor[1]));
            }else{
                $map['r.floor'] = $data['floor'];
            }

        }
        if (!empty ($data['f_total'])){
            $f_total = explode('-', $data['f_total']);
            if (count ($f_total) == 2){
                $map['bu.height'] = array('between',array ($f_total[0],$f_total[1]));
            }else{
                $map['bu.height'] = $data['f_total'];
            }

        }
        if (!empty ($data['status']) && $data['status']!=0) $map['u.status'] = $data['status'];
        $money = empty ($data['price'])? $data['rent']:$data['price'];
        if (!empty ($money)){
            $price = explode('-', $money);
            if (count ($price) == 2){
                $map['u.total_price'] = array('between',array ($price[0],$price[1]));
            }else{
                $map['u.total_price'] = $money;
            }
        }
        if (!empty ($data['area'])) {
            $map['u.area'] = $data['area'];

            $area = explode('-', $data['area']);
            if (count ($area) == 2){
                $map['u.area'] = array('between',array ($area[0],$area[1]));
            }else{
                $map['u.area'] = $data['area'];
            }
        }

        if (!empty ($data['chamber']))  $map['u.chamber'] = $data['chamber'];
        if (!empty ($data['hall']))  $map['u.hall'] = $data['hall'];
        if (!empty ($data['kitchen']))  $map['u.kitchen'] = $data['kitchen'];
        if (!empty ($data['toilet']))  $map['u.toilet'] = $data['toilet'];

        $map['u.status'] = array ('neq', 4);
        $table = array ('yk_used'=>'u');
        $order = $data['term'].' '.$data['order'];
        $model = M ('used');
        $model->field ('u.id');
        $model->join ('yk_room r ON u.dict_id=r.rid', 'LEFT');
        $model->join ('yk_build bu ON bu.bid=r.build', 'left');
        $model->join ('yk_user user ON u.user_id=user.id', 'left');
        $model->table ($table);
        $model->where ($map);
        $count = $model->count ();//echo $model->getLastSql();die;
        if($page){
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model->field ('u.*,bu.alias as build,r.floor as fool,r.alias as room,user.username');
        $model->join ('yk_room r ON u.dict_id=r.rid', 'LEFT');
        $model->join ('yk_build bu ON bu.bid=r.build', 'left');
        $model->join ('yk_user user ON u.user_id=user.id', 'left');
        $model->table ($table);
        $model->where ($map);
        $model->order ($order);
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model->select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取操作记录
    static public function getUserRecord($pageSize=15,$where=array(),$page=''){
        $arr = array();
        if($page){
            $count = M("user_record")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("user_record");
        $model->where($where);
        $model->order('id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取消息
    static public function getUserMessage($pageSize=15,$where=array(),$page=''){
        $arr = array();
        if($page){
            $count = M("user_message")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("user_message");
        $model->where($where);
        $model->order('id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取文章列表
    static public function getArticleList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("article as a")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("article as a");
        $model->join('left join '.$fex.'article_cate as ac on a.cate_id = ac.id');
        $model->field('ac.name,a.id,a.title,a.hits,a.status,a.add_time,a.user_id');
        $model->where($where);
        $model->order('a.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取收藏列表
    static public function getCollectionList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("house_collection as hc")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("house_collection as hc");
        $model->join('left join '.$fex.'house as h on hc.house_id = h.id');
        $model->field('hc.*,h.title,h.img,h.average_price,h.group_discount,h.discount');
        $model->where($where);
        $model->order('hc.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取关注用户列表
    static public function getFollowList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("user_follow as uf")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("user_follow as uf");
        $model->join('left join '.$fex.'user as u on u.id = uf.follow_id');
        $model->join('left join '.$fex.'user_info as ui on ui.user_id = uf.follow_id');
        $model->field('u.username,uf.*,ui.head_ico');
        $model->where($where);
        $model->order('uf.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取中意楼盘列表
    static public function getItalyList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("house_italy as hi")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("house_italy as hi");
        $model->join('left join '.$fex.'house as h on hi.house_id = h.id');
        $model->field('hi.*,h.title,h.img,h.average_price,h.group_discount,h.discount');
        $model->where($where);
        $model->order('hi.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取反馈列表
    static public function getOpinionList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        if($page){
            $count = M("opinion")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("opinion");
        $model->where($where);
        $model->order('id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取客源列表
    static public function getCustList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        $fex = C('DB_PREFIX');
        if($page){
            $count = M("customs as a")->where($where)->count();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("customs as a");
        $model->join('left join '.$fex.'user as ac on a.user_id = ac.id');
        $model->join('left join '.$fex.'estate as es on a.estate_id = es.id');
        $model->field('a.id,a.title,a.gender,a.contact,a.demand,a.state,a.add_time,a.alert_time,a.user_id,a.leve,ac.username,es.id as eid,es.title as etitle');
        $model->where($where);
        $model->order('a.add_time desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }

    //获取小区列表
    static public function getEstateList($pageSize=15,$where=array(),$page=''){
        $arr = array();
        if($page){
            $count = M("estate as a")->where($where)->count();
            //echo  M("estate as a")->getLastSql();
            $pager = new \Org\Util\Page($count, $pageSize);
            $arr['count'] = $count;
        }
        $model = M("estate as a");
        $model->field('a.id,a.title,a.area_id,a.address,a.years,a.status');
        $model->where($where);
        $model->order('a.id desc');
        if($page){
            $model->limit($pager->firstRow.','.$pager->listRows);
        }else{
            $model->limit($pageSize);
        }
        $arr['list'] = $model-> select();
        if($page){
            $arr['page'] = $pager->show();
        }
        return $arr;
    }
    //用户记录设置
    static public function setRecord ($msg,$type,$user){
        $model = M ('follow');
    }
    //增减积分
    static public function IntegralIncOrSub($amount, $type=1){
        $r_model = M ('integral_rule');
        $a_model = M ('user_account');
        $red_model = M ('user_record');
        //查询积分数量



        $map = array ('');
       /* $model->where('id=5 and type='.$type)->setInc('quota',$amount);
        $model->where('id=5 and type='.$type)->setDec('quota',$amount);
        //添加操作记录*/
        M ('user_record')->add ();
    }
    //增减少活跃度
    static public function ActiveIncOrSub(){
        $model = M ('integral_rule');
    }
}


